//
//  RXFrameDefine.h
//  RXFrame
//
//  Created by srxboys on 2021/4/21.
//

#ifndef RXFrameDefine_h
#define RXFrameDefine_h

#ifdef __OBJC__
//此方法隔离 OC库的导入，防止工程中添加C/C++ 文件时 这里报错
    #import <UIKit/UIKit.h>
    #import <RXFrame/RXFrameLogColor.h>
    #import <RXFrame/NSObject+RXAlloc.h>
#endif

/********************** 通用常量 *************************/
#define CONST_NET_NONE               @"const_network_none"
#define CONST_NET_UNKOWN             @"const_network_unknow"
#define CONST_NET_WIFI               @"const_network_wifi"
#define CONST_NET_PHONE              @"const_network_phone"

#define CONST_LANGUAGE_ZN            @"zh-Hans"
#define CONST_LANGUAGE_EN            @"en"



/********************** 通知定义 ***************************/
#pragma mark  -- 通知定义 --
#define NOTINAME_APNS_PUSH          @"APP_ApnsPush_noti"  //推送消息--通知【带值】
#define NOTINAME_NETWORK            @"App_network_status" //网络状态

/********************** 沙盒 ******************************/
#pragma mark  -- 沙盒 --
#define Sandbox_APP_UDID            @"Sandbox_SCM_APP_UDID"  //UDID
#define Sandbox_USER_ID             @"Sandbox_UserLoginedId" //userID
#define Sandbox_ACCESS_TOKEN        @"Sandbox_ACCESS_TOKEN"  //accessToken
#define Sandbox_APP_LANGUAGE        @"Sandbox_app_language"

/********************** 缓存定义 ***************************/
#pragma mark  -- 缓存定义 --
#define CACHE_ADDRESS_LOCALJSON     @"AdressLocalJson.json" //网络请求地址 --缓存


#pragma mark ---- notifacation 通知定义 --------
/**
 ----------------------【通知 定义】------------------------------------------
 *
 * 全局通知 (不包含 kvc kvo)
 */
#define RX_NOTI_ADD_OBNAME(eumMethod,obserName)    [[NSNotificationCenter defaultCenter] addObserver:self selector:eumMethod name:obserName object:nil]

#define RX_NOTI_POST(obserName) [[NSNotificationCenter defaultCenter] postNotificationName:obserName object:nil userInfo:nil]

#define RX_NOTI_POST_OBNAME(obserName,obserObject,dataInfo)    [[NSNotificationCenter defaultCenter] postNotificationName:obserName object:obserObject userInfo:dataInfo]

#define RX_NOTI_REMOVE_OBNAME(obserObject,obserName)       [[NSNotificationCenter defaultCenter] removeObserver:obserObject name:obserName object:nil]

#define RX_NOTI_REMOVE_SELF [[NSNotificationCenter defaultCenter] removeObserver:self]


/**
 ----------------------【沙盒目录】------------------------------------------
 *
 * 全局通知 (不包含 kvc kvo)
 */
#define RX_LibraryCache() [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#define RX_LibraryFile(_fileName) [NSString stringWithFormat:@"%@/%@",RX_LibraryCache(),_fileName]

#define RX_DocumentsPath()  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
#define RX_DocumentsFile(_fileName) [NSString stringWithFormat:@"%@/%@",RX_DocumentsPath(),_fileName]








/*
 * 控制器 类名
 */
#define RX_SELF_CLASS_NAME  NSStringFromClass([self class])

/*
 * 类名 进行 [[class alloc] init] 实例化 (主要是替换工程 new方法)
 */
#define RXCREATE_ALLOC(className) [NSObject classNameCreateAlloc:@#className]
#define RXCREATE_ALLOC_STR(className) [NSObject classNameCreateAlloc:NSStringFromClass(className)]

/*
 *  弱引用
 */
#define rx_weak(weakSelf)  __weak __typeof(&*self)weakSelf = self
//#define WEAKSELF  typeof(self) __weak weakSelf=self

/**************************************************/
/*头2个是定义*/
#undef rx_weakify
#define rx_weakify(v) __weak typeof(v) weak_##v = v;

#undef rx_strongify
#define rx_strongify(v) __strong typeof(weak_##v) v = weak_##v;
/*配合上面2个使用*/
#undef rx_blockify
#define rx_blockify(v) (weak_##v)
/**************************************************/

// 是否iPad
#define rx_isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

// 是否iPad
#define rx_someThing (isPad) ? ipad : iphone

//判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif

#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif


///多语言 根据当前语言环境取到对应字符
#define RX_Localized(key)  [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"Sandbox_app_language"]] ofType:@"lproj"]] localizedStringForKey:(key) value:nil table:@"Localizable"]

#define RX_LocSource(key)  [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"Sandbox_app_language"]] ofType:@"lproj"]] localizedStringForKey:(key) value:nil table:@"LocSource"]


//获取当前语言
#define RX_CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

#pragma mark ---- iOS 版本 定义 --------
//当前设备
#define RX_CurrentDevice [UIDevice currentDevice]

//操作系统版本
#define RX_SYSTEMVERSION   [UIDevice currentDevice].systemVersion

//大于多少版本
#define RX_iOS7OrLater ([RX_SYSTEMVERSION floatValue] >= 7.0)
#define RX_iOS8OrLater ([RX_SYSTEMVERSION floatValue] >= 8.0)
#define RX_iOS9OrLater ([RX_SYSTEMVERSION floatValue] >= 9.0)
#define RX_iOS10OrLater ([RX_SYSTEMVERSION floatValue] >= 10.0)
#define RX_iOSVLater(__version) ([RX_SYSTEMVERSION floatValue] >= __version)
#define RX_iOSAVALIABLE(__min, __max) ([RX_SYSTEMVERSION floatValue] >= __min && [RX_SYSTEMVERSION floatValue] < __max)

//系统字体
#define RX_FontBase(size) [UIFont systemFontOfSize:size]
#define RX_FontBold(size) [UIFont boldSystemFontOfSize:size]


#define RX_ImageName(kStr) [UIImage imageNamed:kStr]

// 沙盒存取
#define RX_UserSaveSetting(key, value)\
NSUserDefaults *def =[NSUserDefaults standardUserDefaults];\
[def setObject:value forKey:key];\
[def synchronize];


#define RX_UserGetSetting(key)   [[NSUserDefaults standardUserDefaults] objectForKey:key]

#define RX_UserRemoveSetting(key) \
NSUserDefaults *def =[NSUserDefaults standardUserDefaults];\
[def removeObjectForKey:key];\
[def synchronize];



#define RX_SAVE_ACCESSTOKEN(__accessToken) \
__accessToken = StrFormatWhiteSpace(__accessToken); \
if(StrBool(__accessToken)) { \
RX_UserSaveSetting(@"Sandbox_ACCESS_TOKEN", __accessToken);\
}

#define RX_GET_ACCESSTOKEN \
NonEmptyString((NSString *)RX_UserGetSetting(@"Sandbox_ACCESS_TOKEN"))


#define RX_SharedAppKeyWindow [UIApplication sharedApplication].keyWindow
#define RX_SharedAppDelegate ((AppDelegate*)[[UIApplication sharedApplication] delegate])

/**
 * 普通跳转 url
 * @note 参考: https://blog.csdn.net/u011619283/article/details/53331597
 *
 * 跳转到 其他app
 * @note 参考: https://www.jianshu.com/p/6f3d8a80acc0
 */
#define RX_APPPOpenURLOptions(_url, _options, callback) \
    [[UIApplication sharedApplication] openURL:_url options:_options completionHandler:^(BOOL success) { \
        if (callback) { callback(success); } \
    }]
#define RX_APPOpenURL(_url) RX_APPPOpenURLOptions(_url, @{}, nil)
#define RX_APPOpenURLString(_url) RX_APPOpenURL([NSURL URLWithString:_url])

/*
 * ------ 状态栏 旋转小菊花 ----
 */
#define RX_STATUS_BAR_START() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define RX_STATUS_BAR_STOP() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO

/*
 * ------ 宽 高 定义 ----
 */
#pragma mark ---- 宽 高 定义 --------
#define RX_ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define RX_ScreenHeight [UIScreen mainScreen].bounds.size.height

#define RX_SystemStatusHeight [[UIApplication sharedApplication] statusBarFrame].size.height

//宽高比定义  roundf 四舍五入函数 --针对 iPhone6设计稿适配
#define RX_ActureHeight(_height)  roundf(_height/375.0 * RX_ScreenWidth)
#define RX_ActureHeightV(_height) roundf(_height/667.0 * RX_ScreenHeight)

//--针对 iPhone7 Plus 设计稿适配
#define RX_LAYOUT_HEIGHT(_height)  roundf(_height/540.0 * RX_ScreenWidth)
#define RX_LAYOUT_HEIGHTV(_height) roundf(_height/960.0 * RX_ScreenHeight)


// NSString *a = RX_STRING_FORMAT(@"word %d %f", 4, 2.5);
#define RX_STRING_FORMAT(str, ...) [NSString stringWithFormat:str, __VA_ARGS__]
#define RX_STRING_OF_FLOAT(num) [NSString stringWithFormat:@"%.2f", num]
#define RX_STRING_OF_INT(num) [NSString stringWithFormat:@"%d", num]


#define __MIN_DOUBLE 0.001
#define RX_THAN_LESS(A, B) ((A) + __MIN_DOUBLE <= (B))
#define RX_THAN_GREATER(A, B) ((A) >= __MIN_DOUBLE + (B))



#pragma mark  ---- Frame & Bound ---------

#define RX_FRAME_L(view) (view.frame.origin.x)
#define RX_FRAME_R(view) (view.frame.origin.x + view.frame.size.width)
#define RX_FRAME_T(view) (view.frame.origin.y)
#define RX_FRAME_B(view) (view.frame.origin.y + view.frame.size.height)
#define RX_FRAME_W(view) (view.frame.size.width)
#define RX_FRAME_H(view) (view.frame.size.height)

#define RX_BOUND_L(view) (view.bounds.origin.x)
#define RX_BOUND_R(view) (view.bounds.origin.x + view.bounds.size.width)
#define RX_BOUND_T(view) (view.bounds.origin.y)
#define RX_BOUND_B(view) (view.bounds.origin.y + view.bounds.size.height)
#define RX_BOUND_W(view) (view.bounds.size.width)
#define RX_BOUND_H(view) (view.bounds.size.height)


#pragma mark ---- 单例 --------
/*
 ----- 单例 -------
 */
#define RX_DEFINE_SINGLETON_FOR_HEADER(className)\
+(className* )shared##className;
#define DEFINE_SINGLETON_FOR_CLASS(className)\
+ (className *)shared##className {\
    static className *shared##className = nil;\
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        @synchronized(self){\
            shared##className = [[self alloc] init];\
        }\
    }); \
    return shared##className; \
}





#ifdef __OBJC__
    #import <RXFrame/RXHexColor.h>
#endif

#define RXColorHexStr(_color) [RXHexColor colorWithHexString:_color]
#define RXColorHexStrAlpha(_color,__alpha) [RXHexColor colorWithHexString:_color alpha:__alpha]

#define RXColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define RXIColorHexAlpha(rgbValue,__alpha) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(__alpha)]

// 随机色
#define RXColorRandom UIColorRGB(arc4random_uniform(256), \
                        arc4random_uniform(256), \
                            arc4random_uniform(256))


#define RXColorRGB(r, g, b)     [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]

#define RXColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)/255.0]

#define RXColorHSL(h, s, l)     [UIColor colorWithHue:(h)/255.0f saturation:(s)/255.0f brightness:(l)/255.0f alpha:1.0f]

#define RXColorHSLA(h, s, l, a) [UIColor colorWithHue:(h)/255.0f saturation:(s)/255.0f brightness:(l)/255.0f alpha:(a)/255.0f]

#define RX_COLOR_LINE   RXColorHexStr(@"DCDCDC")
#define RX_COLOR_EEE    RXColorRGB(238, 238, 238)
#define RX_COLOR_DDD    RXColorRGB(221, 221, 221)

#define RX_COLOR_333    RXColorRGB(51 , 51 , 51 )
#define RX_COLOR_666    RXColorRGB(102, 102, 102)
#define RX_COLOR_999    RXColorHexStr(@"999999")

#endif /* RXFrameDefine_h */
